package com.fs.sale.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.page.PageDomain;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.core.vo.AjaxResult;
import com.fs.common.util.page.PageUtils;
import com.fs.sale.service.InventoryService;
import com.fs.sale.service.ProductService;
import com.fs.sale.vo.ProductVo;
import com.fs.sale.vo.UpdateInventoryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("product")
public class ProductController extends BaseController {
    @Autowired
    ProductService productService;

    @Autowired
    InventoryService inventoryService;

    @PostMapping("list")
    public AjaxResult selectAll(){

        AjaxResult ajaxResult=new AjaxResult();
        List<ProductVo> productList=productService.selectAll();
        ajaxResult.put("productList",productList);
        return ajaxResult;
    }

    @PostMapping("select")
    public TableDataInfo select(@RequestBody ProductVo productVo){
        PageDomain pageDomain = PageUtils.startPage();
        if(Objects.nonNull(productVo.getCurrentPage()))
            pageDomain.setPageNum(productVo.getCurrentPage());
        if(Objects.nonNull(productVo.getPageSize()))
            pageDomain.setPageSize(productVo.getPageSize());
        Page<ProductVo> page = productService.select(pageDomain,productVo);
        return this.getDataTable(page);
    }

    @PostMapping("selectProduct")
    public AjaxResult selectProduct(){
        AjaxResult ajaxResult=new AjaxResult();
        ajaxResult.put("productList",productService.selectProduct());
        return ajaxResult;
    }


    @PostMapping("queryBrand")
    public AjaxResult queryBrand(){
        AjaxResult ajaxResult=new AjaxResult();
        List<String> brandList=productService.queryBrand();
        ajaxResult.put("brandList",brandList);
        return ajaxResult;
    }

    @PostMapping("queryProductName")
    public AjaxResult queryProductName(@RequestParam(value = "brand",required = false) String brand){
        AjaxResult ajaxResult=new AjaxResult();
        List<String> productNameList=productService.queryProductName(brand);
        ajaxResult.put("productNameList",productNameList);
        return ajaxResult;
    }

    @PostMapping("updateInventory")
    public AjaxResult updateInventory(@RequestBody UpdateInventoryVo updateInventoryVo){

        if(inventoryService.updateInventory(updateInventoryVo)>0){
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }


}
